home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / RemoteCommand / Source / RemoteCommand.m < prev    next >
Encoding:
Text File  |  1993-06-11  |  5.2 KB  |  175 lines

  1. // -------------------------------------------------------------------------------------
  2. // RemoteCommand
  3. // -------------------------------------------------------------------------------------
  4. // Permission is granted to freely redistribute this source code, and to use fragments
  5. // of this code in your own applications if you find them to be useful.  This class,
  6. // along with the source code, come with no warranty of any kind, and the user assumes
  7. // all responsibility for its use.
  8. // -------------------------------------------------------------------------------------
  9.  
  10. #import <appkit/appkit.h>
  11. #import <libc.h>
  12. #import <stdlib.h>
  13. #import <string.h>
  14. #import <c.h>
  15. #import <pwd.h>
  16. #import <sys/param.h>
  17. #import <sys/types.h>
  18. #import <sys/time.h>
  19. #import <sys/dir.h>
  20. #import <remote/NXConnection.h>
  21. #import "RemoteCommand.h"
  22. #import "ExecServer.h"
  23. #import "userInfo.h"
  24. #import "AskOperator.h"
  25. #import "ExecMonitor.h"
  26.  
  27. // -------------------------------------------------------------------------------------
  28. // return path to application ("/appName" is removed from path)
  29. // Note: This should be called at the beginning of the app to cache the app path name
  30. const char *XAppPath()
  31. {
  32.     static char    *appPathName = (char*)nil;
  33.     if (!appPathName) {
  34.         char *p, path[MAXPATHLEN + 1];
  35.         if (NXArgv[0][0] == '/') strcpy(path, NXArgv[0]);
  36.         else { 
  37.             getwd(path);
  38.             strcat(path, "/");
  39.             strcat(path, NXArgv[0]);
  40.         }
  41.         if ((p = rindex(path, '/')) && (p != path)) *p = 0;
  42.         appPathName = strcpy((char*)malloc(strlen(path) + 1), path);
  43.     }
  44.     return appPathName;
  45. }
  46.  
  47. // -------------------------------------------------------------------------------------
  48. @interface _RmtMethods : Object
  49. - (const char*)getResponseForMessage:(askOperator_t*)ao;
  50. @end
  51.  
  52. // -------------------------------------------------------------------------------------
  53. @implementation RemoteCommand
  54.  
  55. // -------------------------------------------------------------------------------------
  56. // create new ExecMonitor
  57. // -------------------------------------------------------------------------------------
  58.  
  59. /* "Continue" select from NewExecMonitor panel (or "Cancel" if tag != 0) */
  60. - newExecOk:sender
  61. {
  62.     [newExecWindow orderOut:self];
  63.     if (![sender tag]) {
  64.         const char *host = [newExecHost stringValue], *server = [newExecServer stringValue];
  65.         if (![ExecMonitor newExecHost:host server:server]) {
  66.             char *errMsg = "Unable to connect to server name \"%s\" on host \"%s\".  "
  67.                            "'RemoteRunServer' may not have run successfully.";
  68.             NXRunAlertPanel("Error", errMsg, "Continue",0,0, server, host);
  69.         }
  70.     }
  71.     return self;
  72. }
  73.  
  74. /* new monitor */
  75. - newExecMonitor:sender
  76. {
  77.     char    localHost[MAXHOSTNAMELEN + 1], serverName[256];
  78.     gethostname(localHost, sizeof(localHost));
  79.     sprintf(serverName, "ExecServer_%s_%d", localHost, getpid());
  80.     [newExecServer setStringValue:serverName];
  81.     [newExecHost setStringValue:""];
  82.     [newExecWindow center];
  83.     [newExecWindow makeKeyAndOrderFront:(id)nil];
  84.     return self;
  85. }
  86.  
  87. // -------------------------------------------------------------------------------------
  88. // menu actions
  89. // -------------------------------------------------------------------------------------
  90.  
  91. /* application termination */
  92. - terminate:sender
  93. {
  94.     return [super terminate:sender];
  95. }
  96.  
  97. // -------------------------------------------------------------------------------------
  98. // app name info
  99. // -------------------------------------------------------------------------------------
  100.  
  101. - (const char*)appUserName
  102. {
  103.     return appUserName;
  104. }
  105.  
  106. - (const char*)appUserHome
  107. {
  108.     return appUserHome;
  109. }
  110.  
  111. - (const char*)appServerHost
  112. {
  113.     return appServerHost;
  114. }
  115.  
  116. - (const char*)appServerName
  117. {
  118.     return appServerName;
  119. }
  120.  
  121. // -------------------------------------------------------------------------------------
  122. // application initialization
  123. // -------------------------------------------------------------------------------------
  124.  
  125. /* application initialization */
  126. - appDidInit:sender
  127. {
  128.     id            rmtMethods;
  129.     char        userName[256], userHome[MAXPATHLEN + 1];
  130.     char        host[MAXHOSTNAMELEN + 1], server[256];
  131.     
  132.     /* run-time initialization */
  133.     XAppPath();
  134.  
  135.     /* get user name */
  136.     if (!XMyUserName(userName) || !XUserHomeDirectory(userName, userHome)) {
  137.         NXLogError("[RemoteCommand] Unable to get current user name/home");
  138.         [self terminate:(id)nil];
  139.         exit(2);
  140.     }
  141.     appUserName = NXCopyStringBuffer(userName);
  142.     appUserHome = NXCopyStringBuffer(userHome);
  143.  
  144.     /* remote methods object for _askop (never freed) */
  145.     rmtMethods = [[_RmtMethods alloc] init];
  146.     
  147.     /* get serverName and register for _askop server */
  148.     gethostname(host, sizeof(host));
  149.     appServerHost = NXCopyStringBuffer(host);
  150.     sprintf(server, "%s_%s_%d", ASK_SERVER, appServerHost, getpid());
  151.     appServerName = NXCopyStringBuffer(server);
  152.     [[NXConnection registerRoot:rmtMethods withName:appServerName] runFromAppKit];
  153.  
  154.     /* new exec monitor */
  155.     [self newExecMonitor:self];
  156.     
  157.     return self;
  158. }
  159.  
  160. @end
  161.  
  162. // -------------------------------------------------------------------------------------
  163. // Remote methods (called by '_askop')
  164. // -------------------------------------------------------------------------------------
  165. @implementation _RmtMethods
  166.  
  167. /* show panel and get user response */
  168. - (const char*)getResponseForMessage:(askOperator_t*)ao
  169. {
  170.     return [AskOperator showAskPanel:ao];
  171.     // free ao ?
  172. }
  173.  
  174. @end
  175.